package com.zy.aspects;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.zy.web.bean.MyUserDTO;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @program: shopping153_cloud
 * @description:
 * @author: zy
 * @create: 2025-08-30 17:19
 */

@Component
@Log
public class JmsMessageProducer {

    @Value("${spring.activemq.queueName}")
    private String queueName="userLogin";

    @Autowired
    private JmsTemplate jmsTemplate;   //jms的操作模板类   . 类比:  JdbcTemplate,

    public void sendMessage(MyUserDTO myUserDTO){
        //将对象数据转为json数据.
        myUserDTO.setUserPassword(null);
        myUserDTO.setAvatar(   null );
        myUserDTO.setAvatarFile(null );
        myUserDTO.setOptime(   new Date() );

        Gson gson=new Gson();
        //将对象中null值的数据排除。

        String jsonStr= gson.toJson(      myUserDTO);

        jmsTemplate.convertAndSend(queueName,jsonStr); // 将myUserDTO的数据发出.
        log.info("发送消息到activemq队列"+queueName+",消息为："+ jsonStr);


    }
}
